#include "Finder.h"
#include "Scintilla.h"
#include  "WcharMbcsConvertor.h"
#include <tchar.h>
#include "Constants.h"

const char* SERVER_CALLS_FIND_STRING =   "::LoadScreen\\(.*?/ion_fut.*,";

Finder::Finder(HWND pScintillaPtr)
    :_lastFoundInfo(NULL)
{
    _pScintillaPtr = pScintillaPtr;
    _results.reserve(100);
}

Finder::~Finder()
{

}

LRESULT Finder::execute(UINT Msg, WPARAM wParam/* =0 */, LPARAM lParam/* =0 */) const
{
    return ::SendMessage(_pScintillaPtr, static_cast<int>(Msg), static_cast<int>(wParam), static_cast<int>(lParam));
}

void Finder::FindServerCalls(FindCallback callback)
{
    initSearch();

    int fromPos = 0;
    int endPos = static_cast<int>(execute(SCI_GETLENGTH,0,0));

    bool result = false;
    do
    {        
        result = DoSearch(SERVER_CALLS_FIND_STRING,fromPos,endPos);
        fromPos = GetLastFoundInfo()._start+20; // TO DO
        if (result)
        {
            callback();
        }
    }while (result == true);
    
}

void Finder::initSearch()
{
    _results.clear();

    int flags = (SCFIND_REGEXP|SCFIND_POSIX) | SCFIND_REGEXP_SKIPCRLFASONE | SCFIND_REGEXP_EMPTYMATCH_NOTAFTERMATCH;
    execute(SCI_SETSEARCHFLAGS, static_cast<WPARAM>(flags),0);
}

bool Finder::DoSearch(const char* txtToFind, int fromPos, int endPos)
{
    execute( SCI_SETTARGETSTART, static_cast<WPARAM>(fromPos),0);
        execute( SCI_SETTARGETEND, static_cast<WPARAM>(endPos),0);

    int text2FindLen = strlen(txtToFind);
    int targetStart = execute( SCI_SEARCHINTARGET,  static_cast<WPARAM>(text2FindLen), reinterpret_cast<LPARAM>(txtToFind));    
    
    if (targetStart == -1 || targetStart == -2)
    {
        return false;
    }
    int targetEnd =   execute( SCI_GETTARGETEND,  0, 0);

    int foundTextLen = targetEnd - targetStart;

    int lineNumber = execute( SCI_LINEFROMPOSITION, static_cast<WPARAM>(targetStart),0);
    int lend = execute( SCI_GETLINEENDPOSITION, static_cast<WPARAM>(lineNumber),0);
    int lstart = execute( SCI_POSITIONFROMLINE, static_cast<WPARAM>(lineNumber),0);\

    //look for server call here actually
    TCHAR lineBuf[1024];
    int start_mark = targetStart - lstart;
    int end_mark = targetEnd - lstart;

    char *destA = new char[targetEnd - targetStart + 1];

    TextRange tr;
    tr.chrg.cpMin = targetStart;
    tr.chrg.cpMax = targetEnd;
    tr.lpstrText = destA;
    execute(SCI_GETTEXTRANGE, 0, reinterpret_cast<LPARAM>(&tr));

    int cp = execute(SCI_GETCODEPAGE, 0, 0);
    WcharMbcsConvertor* wmc = WcharMbcsConvertor::getInstance();
    const TCHAR *destW = wmc->char2wchar(destA, cp, &start_mark, &end_mark);
    _tcsncpy_s(lineBuf, 1024, destW, _TRUNCATE);
    delete [] destA;

    generic_string line = lineBuf;
    line += TEXT("\r\n");
//    SearchResultMarking srm;
//    srm._start = start_mark;
//    srm._end = end_mark;

    _results.push_back(FoundInfo(targetStart,targetEnd,lineNumber + 1,line.c_str(), TEXT("")));

    return true;
}

const FoundInfo& Finder::GetLastFoundInfo() const
{
    return _results.back();
}